// pages/examService/create/create.js
const { request } = require('../../../utils/request');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    course:'',
    textRoom:'',
    sit:'',
    courseTS:false,
    textRoomTS:false,
    sitTS:false,
    blue:'',
    date1:'2025-07-01',
    time1:'09:30',
    date2:'2025-07-01',
    time2:'12:00',
    timeTS:false
  },
  handleInput(e){
    let type=e.currentTarget.id
    this.setData({
      [type]:e.detail.value.trim()
    })
    let {course,textRoom,sit,courseTS,textRoomTS,sitTS}=this.data
    if(course&&textRoom&&sit){
      this.setData({
        blue:'blue'
      })
    }else{
      this.setData({
        blue:''
      })
    }

    if(courseTS||textRoomTS||sitTS){
     
      // 科目判断
          if(course.length<20){
            this.setData({
              courseTS:false
            })
          }
          //考场判断
          if(textRoom.length<20){
            this.setData({
              textRoomTS:false
            })
          }
          //座位判断
          let sitReg=/[0-9a-zA-Z\u4e00-\u9fa5]/
          if(sitReg.test(sit)&&sit.length<=8){
            this.setData({
              sitTS:false
            })  
          }
    
    }
    
  },

  handleCheck(){
    let {course,textRoom,sit,blue,courseTS,textRoomTS,sitTS,timeTS,date1,date2,time1,time2}=this.data
   if(!blue){
      return;
   }

    // 科目判断
    if(course.length>20){
      courseTS=true
      this.setData({
        courseTS:true
      })
    }
    //考场判断
    if(textRoom.length>20){
      textRoomTS=true
      this.setData({
        textRoomTS:true
      })
    }
    //座位判断
    let sitReg=/[A-Za-z0-9\u4e00-\u9fa5]/ 
    if(!sitReg.test(sit)||sit.length>8){
      sitTS=true
      this.setData({
        sitTS:true
      })
    }
    //日期
    if(date1>date2||(date1==date2&&time1>time2)){
      timeTS=true
      this.setData({
        timeTS:true
      })
    }
    
    //发送请求
    if (!courseTS&&!sitTS&&!timeTS&&!textRoomTS) {
      console.log("发送成功");
      let d1 = new Date(date1+' '+time1+':00')
      let d2 = new Date(date2+' '+time2+':00')
      request({
        url:'addExam',
        method:'POST',
        data:{
          exam:{
            "name":course,
            "addr":textRoom,
            "startTime":d1.getTime()/1000,
            "endTime":d2.getTime()/1000,
            "seat":sit
          }
        }
      }).then((res)=>{
        wx.reLaunch({
          url: '/pages/examService/list/list',
        })
      }).catch((err)=>{
        console.log(err);
      })
    }
   
  },
  // 日期
  bindDateChange1(e){
      this.setData({
      date1: e.detail.value
    })
    this.handleTime()
  },
  bindTimeChange1(e){
    this.setData({
      time1: e.detail.value
    })
    this.handleTime()
  },
  bindDateChange2(e){
    this.setData({
      date2: e.detail.value
    })
    this.handleTime()
  },
  bindTimeChange2(e){
    this.setData({
      time2: e.detail.value
    })
    this.handleTime()
  },
  handleTime(){
    let {date1,date2,time1,time2,timeTS }=this.data
    if (timeTS) {
      if (date1<date2||(date1==date2&&time1<time2)) { 
        this.setData({
          timeTS:false
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
 
})